package com.tianji.aigc.tools;

import com.tianji.aigc.constant.ToolConstant;
import com.tianji.aigc.entity.CourseInfo;
import com.tianji.api.client.course.CourseClient;
import com.tianji.api.dto.course.CourseBaseInfoDTO;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 课程工具
 *
 * @Author mr.wu
 * @Date 2025-9-3 9:26
 */
@Component
public class CourseTool {

    @Autowired
    private CourseClient courseClient;

    @Tool(description = ToolConstant.Tool.QUERY_COURSE_BY_ID)
    public CourseInfo queryCourseById(@ToolParam(description = ToolConstant.Tool_Param.COURSE_ID) Long id, ToolContext toolContext) {
        //1.跨服务调用查询课程服务中对应的课程详情
        CourseBaseInfoDTO courseBaseInfoDTO = courseClient.baseInfo(id, true);

        //2.封装成大模型需要的数据
        CourseInfo courseInfo = CourseInfo.of(courseBaseInfoDTO);

        //从共享容器里获取请求ID
        String requestId = (String) toolContext.getContext().get(ToolConstant.REQUEST_ID);

        //思路：在这里最好能将课程存入到一个结果保持器中，容器如何设计？线程安全的HashMap:Map<String,Map<String,Object>>
        //key         field                  value
        //请求ID     courseInfo_课程ID      courseInfo对象
        ToolResultHolder.put(requestId,"courseInfo_"+id, courseInfo);

        //需要想办法让会话记忆的add方法里能获取到courseInfo的值

        return courseInfo;
    }
}
